package com.healthbox.appointmentservice.service.impl;

import com.healthbox.appointmentservice.DTO.HospitalDTO;
import com.healthbox.appointmentservice.entity.Hospital;
import com.healthbox.appointmentservice.mapper.HospitalMapper;
import com.healthbox.appointmentservice.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author pengpeng
 * @date 2022/5/12
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalMapper repo;

    /**
     * 持久化检测机构对象，如果, 操作失败返回null，否则返回Hospital
     * @param hospitalDTO
     * @return
     */
    @Override
    public Hospital newHospital(HospitalDTO hospitalDTO) {
        Hospital entity = HospitalDTO.toEntity(hospitalDTO);
        repo.save(entity);
        return entity.getHospitalId()==null? null:entity;
    }

    /**
     * 更新检测机构信息
     * 成功返回Hospital
     * 失败返回null
     * @param hospitalDTO
     * @return
     */
    @Override
    public Hospital updateHospital(HospitalDTO hospitalDTO) {
        Hospital entity = HospitalDTO.toEntity(hospitalDTO);
        int update = repo.update(entity);
        return update==1? entity:null;
    }

    /**
     * 根据条件查询检测机构
     * @param name
     * @param phone
     * @return
     */
    @Override
    public List<Hospital> findHospital(String name, String phone) {
        return repo.findByNameAndPhone(name, phone);
    }

    /**
     * 查询一个手机号对应的检测机构(必须是完整手机号)
     * @param phone
     * @return
     */
    @Override
    public Hospital findOne(String phone){
        List<Hospital> hospitals = repo.findByNameAndPhone(null, phone);
        assert(hospitals.size()<=1);
        if(hospitals.size()==1) return hospitals.get(0);
        return null;
    }
}
